package com.clm.controller.admin;

import com.clm.dto.SetmealDTO;
import com.clm.dto.SetmealPageQueryDTO;
import com.clm.result.PageResult;
import com.clm.result.Result;
import com.clm.service.SetMealService;
import com.clm.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author dinghuapeng
 * @Date 2023/11/2 14:08
 * @PackageName:com.clm.controller.admin
 * @ClassName: SetmealController
 * @Description: TODO
 * @Version 1.0
 */
@RestController
@Slf4j
@Api(tags = "套餐管理")
@RequestMapping("/admin/setmeal")
public class SetmealController {

    @Autowired
    private SetMealService setMealService;

    @PostMapping()
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐:{}",setmealDTO);
        setMealService.save(setmealDTO);
        return Result.success();
    }
    @GetMapping("/page")
    @ApiOperation("套餐分页显示")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("套餐分页显示:{}",setmealPageQueryDTO);
        PageResult pageResult = setMealService.setmealPageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("批量删除菜品")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        log.info("批量删除菜品：{}",ids);
        setMealService.delete(ids);
        return Result.success();
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @ApiOperation("修改套餐")
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐:{}",setmealDTO);
        setMealService.update(setmealDTO);
        return Result.success();
    }

    /**
     * 根据id查询套餐里的菜品
     * @param id
     * @return
     */
    @ApiOperation("根据id查询套餐里的菜品")
    @GetMapping("/{id}")
    public Result<SetmealVO> getByIdWithDish(@PathVariable Long id){//路径注释
        log.info("根据id查询套餐里的菜品:{}",id);
        SetmealVO setmealVO = setMealService.getByIdWithDish(id);
        return Result.success(setmealVO);//最后传的是一个setmealVO
    }


    @ApiOperation("起售或停售套餐")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("起售或停售套餐:{}，{}",status,id);
        setMealService.startOrStop(status,id);
        return Result.success();
    }
}
